package com.sinothk.fc.bill.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.bill.entity.BillAddReq;
import com.sinothk.fc.bill.entity.BillClassifyEntity;
import com.sinothk.fc.bill.entity.BillEntity;
import com.sinothk.fc.bill.entity.BillReq;
import com.sinothk.fc.bill.mapper.BillMapper;
import com.sinothk.fc.bill.service.IBillService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * @Description: 招财猫账单表
 * @Author: Liangyt
 * @Date: 2022-09-12
 * @Version: V1.0
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, BillEntity> implements IBillService {

    @Resource
    private BillMapper billMapper;

    @Override
    public BillEntity add(BillAddReq billAddReq) throws Exception {
        BillEntity bill = new BillEntity();
        BeanUtils.copyProperties(billAddReq, bill);

        bill.setBillDay(DateUtil.format(new Date(), "yyyy年MM月dd日"));
        bill.setBillMonth(bill.getBillDay().substring(0, 8));
        bill.setBillYear(bill.getBillDay().substring(0, 4));

        if (StringUtils.isBlank(bill.getBillType())) {
            bill.setBillType("默认分类");
        }

        if (bill.getBillValue() == null || bill.getBillValue() == 0) throw new Exception("金额异常");
        bill.setBillFlagValue(bill.getBillValue());
        this.save(bill);
        return bill;
    }

    @Override
    public void doPay(BillReq billReq) throws Exception {
        BillEntity bill = new BillEntity();
        BeanUtils.copyProperties(billReq, bill);

        bill.setBillDay(DateUtil.format(new Date(), "yyyy年MM月dd日"));
        bill.setBillMonth(bill.getBillDay().substring(0, 8));
        bill.setBillYear(bill.getBillDay().substring(0, 4));

        if (StringUtils.isBlank(bill.getBillType())) {
            bill.setBillType("默认分类");
        }

        if (bill.getBillValue() == null || bill.getBillValue() == 0) throw new Exception("金额异常[" + bill.getBillValue() + "]");
        bill.setState(1);//"账单状态:0待付款1已付款2已取消"
        this.save(bill);
    }

    @Override
    public void doBathPay(ArrayList<BillReq> billListReq) {
        Date thisDay = new Date();
        ArrayList<BillEntity> billList = new ArrayList<>();
        for (BillReq billReq : billListReq) {
            BillEntity bill = new BillEntity();
            BeanUtils.copyProperties(billReq, bill);
            bill.setBillDay(DateUtil.format(thisDay, "yyyy年MM月dd日"));
            bill.setBillMonth(bill.getBillDay().substring(0, 8));
            bill.setBillYear(bill.getBillDay().substring(0, 4));

            if (StringUtils.isBlank(bill.getBillType())) {
                bill.setBillType("默认分类");
            }
            bill.setState(1);//"账单状态:0待付款1已付款2已取消"
            billList.add(bill);
        }
        this.saveBatch(billList);
    }

    @Override
    public IPage<BillEntity> queryPageListForStore(Page<BillEntity> page) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店ID");
//        wrapper.eq(BillEntity::getStoreId, loginUser.getStoreId());
//        wrapper.orderByDesc(BillEntity::getCreateTime);

        return billMapper.pageListForStoreByDay(page, loginUser.getStoreId());
    }

    @Override
    public IPage<BillClassifyEntity> pageForStoreGroupByOrderId(Page<BillClassifyEntity> page) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店ID");
        return billMapper.pageForStoreGroupByOrderId(page, loginUser.getStoreId());
    }
}
